package com.qyhl.webtv.commonlib.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.service.UserService;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.share.ShareUtils;

import razerdp.basepopup.BasePopupWindow;


public class MShareBoard extends BasePopupWindow implements View.OnClickListener {
    private View popupView;

    private Activity activity;

    private String mNewsId, mTitle, mImg, mDes, mPid;
    private int mType;
    private boolean hasCollection = false;
    private boolean isCollected = false;

    private ShareToolListener toolListener;

    private ShareCallBack shareCallBack;

    public void setShareCallBack(ShareCallBack shareCallBack) {
        this.shareCallBack = shareCallBack;
    }

    public void setToolListener(ShareToolListener toolListener) {
        this.toolListener = toolListener;
    }

    public MShareBoard(Activity context, String newsId, String title, String img, String description, int type, boolean hasFeedback) {
        super(context);
        activity = context;
        mNewsId = newsId;
        mTitle = title;
        mImg = img;
        mDes = description;
        mType = type;
        bindEvent(false, hasFeedback);
    }

    public MShareBoard(Activity context, String newsId, String title, String img, String description, int type, String pId, boolean hasFeedback) {
        super(context);
        activity = context;
        mNewsId = newsId;
        mTitle = title;
        mImg = img;
        mDes = description;
        mType = type;
        mPid = pId;
        bindEvent(false, hasFeedback);
    }

    public MShareBoard(Activity context, String newsId, String title, String img, String description, String pId, int type) {
        super(context);
        activity = context;
        mNewsId = newsId;
        mTitle = title;
        mImg = img;
        mDes = description;
        mPid = pId;
        mType = type;
        bindEvent(false, false);
    }

    public MShareBoard(Activity context, String newsId, String title, String img, String description, String pId, int type, boolean hasCollect, boolean isCollect) {
        super(context);
        activity = context;
        mNewsId = newsId;
        mTitle = title;
        mImg = img;
        mDes = description;
        mPid = pId;
        mType = type;
        hasCollection = hasCollect;
        isCollected = isCollect;
        bindEvent(true, false);
    }

    public MShareBoard(Activity context, String newsId, String title, String img, String description, int type, boolean hasCollect, boolean isCollect, boolean fontSize) {
        super(context);
        activity = context;
        mNewsId = newsId;
        mTitle = title;
        mImg = img;
        mDes = description;
        mType = type;
        hasCollection = hasCollect;
        isCollected = isCollect;
        bindEvent(true, false);
    }


    @Override
    protected Animation initShowAnimation() {
        return getTranslateVerticalAnimation(1f, 0, 500);
    }

    @Override
    protected Animation initExitAnimation() {
        return getTranslateVerticalAnimation(0, 1f, 500);
    }

    @Override
    public View getClickToDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @SuppressLint("InflateParams")
    @Override
    public View onCreatePopupView() {
        popupView = LayoutInflater.from(getContext()).inflate(R.layout.shareboard_popup, null);
        return popupView;
    }

    @Override
    public View initAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent(boolean isTop, boolean hasFeedback) {
        if (popupView != null) {
            popupView.findViewById(R.id.share_qq).setOnClickListener(this);
            popupView.findViewById(R.id.share_wechat).setOnClickListener(this);
            popupView.findViewById(R.id.share_circle).setOnClickListener(this);
            popupView.findViewById(R.id.share_link).setOnClickListener(this);
            popupView.findViewById(R.id.share_text).setOnClickListener(this);
            popupView.findViewById(R.id.share_collect).setOnClickListener(this);
            popupView.findViewById(R.id.share_feedback).setOnClickListener(this);
            popupView.findViewById(R.id.share_feedback_two).setOnClickListener(this);
            popupView.findViewById(R.id.cancel).setOnClickListener(this);
            String advSwitch = (String) AppConfigUtil.getInstance().getConValue(AppConfigConstant.OTHER_ADV);
            if (advSwitch.equals("1")) {
                // TODO 加入广告
            }

            if (isTop) {
                popupView.findViewById(R.id.tool_layout).setVisibility(View.VISIBLE);
                TextView collect = popupView.findViewById(R.id.share_collect);
                if (hasCollection) {
                    if (isCollected) {
                        collect.setText("已收藏");
                        collect.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.share_collect_on_icon), null, null);
                    } else {
                        collect.setText("收藏");
                        collect.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(getContext(), R.drawable.share_collect_off_icon), null, null);
                    }
                    collect.setVisibility(View.VISIBLE);
                    popupView.findViewById(R.id.index_tv).setVisibility(View.GONE);
                } else {
                    collect.setVisibility(View.GONE);
                    popupView.findViewById(R.id.index_tv).setVisibility(View.VISIBLE);
                }
            } else {
                popupView.findViewById(R.id.tool_layout).setVisibility(View.GONE);
            }

            if (hasFeedback) {
                popupView.findViewById(R.id.feedback_layout).setVisibility(View.VISIBLE);
            } else {
                popupView.findViewById(R.id.feedback_layout).setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        dismiss();
        int i = v.getId();
        if (shareCallBack != null && i != R.id.cancel) {
            shareCallBack.callBack();
        }
        if (i == R.id.share_qq) {
            if (mType == 29) {
                ShareUtils.getInstance().goSingleShare("QQ", mNewsId, mPid, mType, mDes, mTitle, mImg, activity);
            } else {
                ShareUtils.getInstance().goSingleShare("QQ", mNewsId, mType, mDes, mTitle, mImg, activity);
            }
        } else if (i == R.id.share_wechat) {
            if (mType == 29) {
                ShareUtils.getInstance().goSingleShare("WeiXin", mNewsId, mPid, mType, mDes, mTitle, mImg, activity);
            } else {
                ShareUtils.getInstance().goSingleShare("WeiXin", mNewsId, mType, mDes, mTitle, mImg, activity);
            }

        } else if (i == R.id.share_circle) {
            if (mType == 29) {
                ShareUtils.getInstance().goSingleShare("Circle", mNewsId, mPid, mType, mDes, mTitle, mImg, activity);
            } else {
                ShareUtils.getInstance().goSingleShare("Circle", mNewsId, mType, mDes, mTitle, mImg, activity);
            }

        } else if (i == R.id.share_link) {
            if (mType == 29) {
                ShareUtils.getInstance().goSingleShare("Link", mNewsId, mPid, mType, mDes, mTitle, mImg, activity);
            } else {
                ShareUtils.getInstance().goSingleShare("Link", mNewsId, mType, mDes, mTitle, mImg, activity);
            }

        } else if (i == R.id.share_collect) {
            if (StringUtils.isNotEmpty(CommonUtils.getInstance().getUserName())) {
                toolListener.collectChange(isCollected);
            } else {
                RouterManager.routerLogin(getContext(), 0);
            }

        } else if (i == R.id.share_text) {
            toolListener.textSizeChange();

        } else if (i == R.id.share_feedback) {
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", mTitle);
                        bundle.putString("id", mNewsId);
                        bundle.putInt("type", mType);
                        RouterManager.router(ARouterPathConstant.NEWS_COMPLAIN, bundle);
                    } else {
                        RouterManager.routerLogin(activity, 0);
                    }
                }

                @Override
                public void error(String s) {

                }
            });


        } else if (i == R.id.share_feedback_two) {
            CommonUtils.getInstance().isLogin(new UserService.LoginCallBack() {
                @Override
                public void login(boolean isLogin) {
                    if (isLogin) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", mTitle);
                        bundle.putString("id", mNewsId);
                        bundle.putInt("type", mType);
                        RouterManager.router(ARouterPathConstant.NEWS_COMPLAIN, bundle);

                    } else {
                        RouterManager.routerLogin(activity, 0);
                    }
                }

                @Override
                public void error(String s) {

                }
            });


        } else if (i == R.id.cancel) {
        }
    }

    /**
     * 生成TranslateAnimation
     *
     * @param durationMillis 动画显示时间
     * @param start          初始位置
     */
    private static Animation getTranslateVerticalAnimation(float start, float end, int durationMillis) {
        Animation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, start, Animation.RELATIVE_TO_PARENT, end);
        translateAnimation.setDuration(durationMillis);
        return translateAnimation;
    }

    public interface ShareToolListener {
        void collectChange(boolean isCollect);

        void textSizeChange();
    }

    public interface ShareCallBack {
        void callBack();
    }

}
